{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "学习目标\n",
    "- 应用pd.concat实现数据的合并\n",
    "- 应用pd.merge实现数据的合并\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "11fcd54e2940b463"
  },
  {
   "cell_type": "markdown",
   "source": [
    "如果你的数据由多张表组成，那么有时候需要将不同的内容合并在一起分析\n",
    "# 1 pd.concat实现数据合并\n",
    "- pd.concat([data1, data2], axis=1)\n",
    "    - 按照行或列进行合并,axis=0为列索引，axis=1为行索引\n",
    "\n",
    "```python\n",
    "# 按照行索引进行\n",
    "pd.concat([data, dummies], axis=1)\n",
    "```"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "2eb1ac4f6180e94e"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 2 pd.merge\n",
    "## 2.1 api介绍\n",
    "- pd.merge(left, right, how='inner', on=None)\n",
    "    - 可以指定按照两组数据的共同键值对合并或者左右各自\n",
    "    - left: DataFrame\n",
    "    - right: 另一个DataFrame\n",
    "    - on: 指定的共同键\n",
    "    - how:按照什么方式连接\n",
    "\n",
    "## 2.2 pd.merge合并案例\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "a0cc8a1e3f96b9fb"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "left = pd.DataFrame({'key1': ['K0', 'K0', 'K1', 'K2'],\n",
    "                        'key2': ['K0', 'K1', 'K0', 'K1'],\n",
    "                        'A': ['A0', 'A1', 'A2', 'A3'],\n",
    "                        'B': ['B0', 'B1', 'B2', 'B3']})\n",
    "\n",
    "right = pd.DataFrame({'key1': ['K0', 'K1', 'K1', 'K2'],\n",
    "                        'key2': ['K0', 'K0', 'K0', 'K0'],\n",
    "                        'C': ['C0', 'C1', 'C2', 'C3'],\n",
    "                        'D': ['D0', 'D1', 'D2', 'D3']})\n",
    "# 默认内连接\n",
    "result = pd.merge(left, right, on=['key1', 'key2'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T09:23:53.378486100Z",
     "start_time": "2024-02-22T09:23:53.374699800Z"
    }
   },
   "id": "9e7fcd9cc8cfb73b",
   "execution_count": 8
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "  key1 key2   A   B   C   D\n0   K0   K0  A0  B0  C0  D0\n1   K1   K0  A2  B2  C1  D1\n2   K1   K0  A2  B2  C2  D2",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key1</th>\n      <th>key2</th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>K0</td>\n      <td>K0</td>\n      <td>A0</td>\n      <td>B0</td>\n      <td>C0</td>\n      <td>D0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>K1</td>\n      <td>K0</td>\n      <td>A2</td>\n      <td>B2</td>\n      <td>C1</td>\n      <td>D1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>K1</td>\n      <td>K0</td>\n      <td>A2</td>\n      <td>B2</td>\n      <td>C2</td>\n      <td>D2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T09:23:56.024894500Z",
     "start_time": "2024-02-22T09:23:56.019924Z"
    }
   },
   "id": "7459e6fe7c8c8a29",
   "execution_count": 9
  },
  {
   "cell_type": "markdown",
   "source": [
    "![](../.images/内连接.png)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "47bd944b9c14f8ee"
  },
  {
   "cell_type": "markdown",
   "source": [
    "- 左连接\n",
    "result = pd.merge(left, right, how='left', on=['key1', 'key2'])\n",
    "- 右连接\n",
    "result = pd.merge(left, right, how='right', on=['key1', 'key2'])\n",
    "- 外链接\n",
    "result = pd.merge(left, right, how='outer', on=['key1', 'key2'])\n",
    "\n",
    "# 3 总结\n",
    "- pd.concat([数据1, 数据2], axis=**)【知道】\n",
    "- pd.merge(left, right, how=, on=)【知道】\n",
    "    - how -- 以何种方式连接\n",
    "    - on -- 连接的键的依据是哪几个"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "aa0a8ca33f60ac17"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
